package com.example.myspringai.controller;


import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/helloworld")
public class HelloworldController {
    private static final String DEFAULT_PROMPT = "你是可爱的汪咪小助手，可能根据提出的问题回答准确的答案！";

    private final ChatClient chatClient;

    @Autowired
    public HelloworldController(ChatClient chatClient) {
        this.chatClient = chatClient;
    }


    /**
     * 流式调用
     * @param question
     * @return
     */
    @RequestMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> stream(String question) {
        return this.chatClient.prompt()
                .system(DEFAULT_PROMPT) // 添加系统提示词以获得更好的回答
                .user(question)
                .stream()
                .content();
    }
}
